package com.shixi.shixitraining.shixitraining.ui.tabhome.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.AuthenDetailActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.HomeGrideAuthenActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.HomeGrideOverseasActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.HomeGrideTrainingActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.OverseasDetailActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.OverseasEmploymentActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.TrainingAuthenActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.TrainingDetailActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.activity.TrainingProjectActivity;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.HomeBannerBean;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.HomeBean;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.HomeGrideBean;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.view.loopPager.LoopViewPagerAdapter;
import com.shixi.shixitraining.shixitraining.view.recycleview.BaseRecycleHolder;
import com.shixi.shixitraining.shixitraining.view.recycleview.MultiItemTypeAdapter;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleDivider;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.List;

/**
 * Created by shenyan on 2018/7/6.
 * 加载不同样式的布局
 */

public class HomeAdapter extends MultiItemTypeAdapter<Object> {
    public static final int TYPE_BANNER = 1;
    public static final int TYPE_HOME_GRIDE = 2;
    public static final int TYPE_TRAINING_GRIDE = 3;
    public static final int TYPE_JOB_LIST = 4;
    public static final int TYPE_AUTHEN_GRIDE = 5;
    private LoopViewPagerAdapter mPagerAdapter;
    private HomeBean.DataBean.TrainingBean trainingBean;


    public HomeAdapter(Context context, List<Object> data) {
        super(context, data);
    }

    @Override
    protected int mulitAapterGetItemViewType(int position, Object object) {

        if (object instanceof List) {
            List list = (List) object;

            if (list.size() > 0 & list != null) {
                if (list.get(0) instanceof HomeBannerBean) {
                    return TYPE_BANNER;
                } else if (list.get(0) instanceof HomeGrideBean) {
                    return TYPE_HOME_GRIDE;
                } else if (list.get(0) instanceof HomeBean.DataBean.TrainingBean) {
                    return TYPE_TRAINING_GRIDE;
                } else if (list.get(0) instanceof HomeBean.DataBean.JobsBean) {
                    return TYPE_JOB_LIST;
                } else if (list.get(0) instanceof HomeBean.DataBean.TrainBean) {
                    return TYPE_AUTHEN_GRIDE;
                }
            }
        }
        return 0;
    }

    /**
     * 绑定布局
     *
     * @param holder   holder
     * @param item     javabean
     * @param position
     */
    @Override
    public void convert(BaseRecycleHolder holder, Object item, int position) {
        int itemViewType = mulitAapterGetItemViewType(position, item);
        if (itemViewType == TYPE_BANNER) {
            bindLooperView(holder, (List<HomeBannerBean>) item, position);
        } else if (itemViewType == TYPE_HOME_GRIDE) {
            bindGrideView(holder, (List<HomeGrideBean>) item, position);
        } else if (itemViewType == TYPE_TRAINING_GRIDE) {
            binTrainingGrideView(holder, (List<HomeBean.DataBean.TrainingBean>) item, position);
        } else if (itemViewType == TYPE_JOB_LIST) {
            bindNewListView(holder, (List<HomeBean.DataBean.JobsBean>) item, position);
        } else if (itemViewType == TYPE_AUTHEN_GRIDE) {
            bindAuthenGrideView(holder, (List<HomeBean.DataBean.TrainBean>) item, position);
        }


    }


    /**
     * 轮播图
     *
     * @param holder
     * @param item
     * @param position
     */
    private void bindLooperView(BaseRecycleHolder holder, final List<HomeBannerBean> item, int position) {
        ViewPager viewPager = holder.getView(R.id.loop_viewpager);
        final ViewGroup indicator = holder.getView(R.id.loop_indicator_point);
        if (viewPager.getAdapter() == null) {
            mPagerAdapter = new LoopViewPagerAdapter(viewPager, indicator);
            viewPager.setAdapter(mPagerAdapter);
            viewPager.addOnPageChangeListener(mPagerAdapter);
            mPagerAdapter.setList(item);
            mPagerAdapter.setLoopViewClickListener(new LoopViewPagerAdapter.LoopViewClickListener() {
                @Override
                public void callback(int position) {
                    ToastUtil.showShortToast(mContext, position + "你被点击啦");
                }
            });
        } else {
            mPagerAdapter.setList(item);
        }
    }

    /**
     * 网格
     *
     * @param holder
     * @param item
     * @param position
     */
    private void bindGrideView(BaseRecycleHolder holder, final List<HomeGrideBean> item, int position) {
        RecyclerView rcview = holder.getView(R.id.rcview_gride);
        GridLayoutManager manager = new GridLayoutManager(mContext, 3);
        rcview.setLayoutManager(manager);
        HomeGrideAdapter adapter = new HomeGrideAdapter(mContext, item, Glide.with(mContext));
        rcview.setAdapter(adapter);
        rcview.addOnItemTouchListener(new RecycleItemClickListener(mContext) {
            @Override
            public void itemClickListener(View view, int position) {
                if (item.get(position).getNum() == 0) {
                    mContext.startActivity(new Intent(mContext, HomeGrideTrainingActivity.class));
                } else if (item.get(position).getNum() == 1) {
                    mContext.startActivity(new Intent(mContext, HomeGrideOverseasActivity.class));
                } else if (item.get(position).getNum() == 2) {
                    mContext.startActivity(new Intent(mContext, HomeGrideAuthenActivity.class));
                }
            }
        });


    }


    /**
     * 热门实训
     *
     * @param holder
     * @param item
     * @param position
     */
    private void binTrainingGrideView(BaseRecycleHolder holder, final List<HomeBean.DataBean.TrainingBean> item, int position) {
        RecyclerView rcview = holder.getView(R.id.traing_rcview);
        GridLayoutManager manager = new GridLayoutManager(mContext, 2);
        rcview.setLayoutManager(manager);
        HomeTrainingGrideAdapter adapter = new HomeTrainingGrideAdapter(mContext, item, Glide.with(mContext));
        rcview.setAdapter(adapter);


        rcview.addOnItemTouchListener(new RecycleItemClickListener(mContext) {
            @Override
            public void itemClickListener(View view, int position) {
                HomeBean.DataBean.TrainingBean trainingBean = item.get(position);
                String trainingId = trainingBean.getId();
                Bundle bundle = new Bundle();
                bundle.putString("id", trainingId);
                Intent intent = new Intent(mContext, TrainingDetailActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);

            }

        });

        TextView mTrainingTv = holder.getView(R.id.taining_more_tv);
        mTrainingTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext, TrainingProjectActivity.class));
            }
        });
    }


    /**
     * 热门岗位
     *
     * @param holder
     * @param item
     * @param position
     */
    private void bindNewListView(BaseRecycleHolder holder, final List<HomeBean.DataBean.JobsBean> item, int position) {

        RecyclerView rcview = holder.getView(R.id.job_rcview);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rcview.setLayoutManager(manager);
        RecycleDivider divider = new RecycleDivider(new ColorDrawable(ContextCompat.getColor(mContext, R.color.grayc5)),
                OrientationHelper.VERTICAL);
        divider.setHeight(mContext, 0.5f);
        rcview.addItemDecoration(divider);
        HomeListAdapter adapter = new HomeListAdapter(mContext, item, Glide.with(mContext));
        rcview.setAdapter(adapter);
        rcview.addOnItemTouchListener(new RecycleItemClickListener(mContext) {
            @Override
            public void itemClickListener(View view, int position) {
                HomeBean.DataBean.JobsBean jobsBean = item.get(position);
                String jobId = jobsBean.getId();
                Bundle bundle=new Bundle();
                bundle.putString("jobId",jobId);
                Intent intent=new Intent(mContext,OverseasDetailActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        TextView mJobTv = holder.getView(R.id.hot_more_tv);
        mJobTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext, OverseasEmploymentActivity.class));
            }
        });

    }


    /**
     * 培训认证
     *
     * @param holder
     * @param item
     * @param position
     */
    private void bindAuthenGrideView(BaseRecycleHolder holder, final List<HomeBean.DataBean.TrainBean> item, int position) {
        RecyclerView rcview = holder.getView(R.id.authen_rcview);
        GridLayoutManager manager = new GridLayoutManager(mContext, 2);
        rcview.setLayoutManager(manager);
        HomeAuthenGrideAdapter adapter = new HomeAuthenGrideAdapter(mContext, item, Glide.with(mContext));
        rcview.setAdapter(adapter);
        rcview.addOnItemTouchListener(new RecycleItemClickListener(mContext) {
            @Override
            public void itemClickListener(View view, int position) {

                HomeBean.DataBean.TrainBean trainBean = item.get(position);
                String trainId = trainBean.getId();
                Bundle bundle=new Bundle();
                bundle.putString("trainId",trainId);
                Intent intent=new Intent(mContext,AuthenDetailActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        TextView mAuthenTv = holder.getView(R.id.authen_more_tv);
        mAuthenTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext, TrainingAuthenActivity.class));
            }
        });

    }


    private void bindText(BaseRecycleHolder holder) {


    }

    /**
     * 返回布局
     *
     * @param viewType
     * @return
     */
    @Override
    protected int getLayoutId(int viewType) {
        if (viewType == TYPE_BANNER) {
            return R.layout.item_home_banner;
        } else if (viewType == TYPE_HOME_GRIDE) {
            return R.layout.item_home_gride;
        } else if (viewType == TYPE_TRAINING_GRIDE) {
            return R.layout.item_home_training;
        } else if (viewType == TYPE_JOB_LIST) {
            return R.layout.item_home_job;
        } else if (viewType == TYPE_AUTHEN_GRIDE) {
            return R.layout.item_home_authen;
        }
        return 0;
    }


}
